package com.example.demo1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo1.common.BtException;
import com.example.demo1.dao.CaseDAO;
import com.example.demo1.entity.Case;
import com.example.demo1.service.CaseService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CaseServiceImpl implements CaseService {

    @Autowired
    CaseDAO caseDao;

    @Override
    public void addCase(Case testCase) {
        if(StringUtils.isNullOrEmpty(testCase.getCasename())){
            BtException.throwException("用例名称为空");
        }
        QueryWrapper<Case> wrapperCase = new QueryWrapper<>();
        wrapperCase.eq("casename",testCase.getCasename());
        if(caseDao.selectCount(wrapperCase)!=0){
            BtException.throwException("用例已存在");
        }
        caseDao.insert(testCase);
    }

    @Override
    public void updateCase(Case testCase) {
        QueryWrapper<Case> wrapperCase=new QueryWrapper<>();
        wrapperCase.eq("id",testCase.getId());
        caseDao.update(testCase,wrapperCase);
    }

    @Override
    public void deleteCase(Case testCase) {
        QueryWrapper<Case> wrapperCase=new QueryWrapper<>();
        wrapperCase.eq("id",testCase.getId());
        caseDao.delete(wrapperCase);
    }

    @Override
    public IPage<Case> getCaseList(int currentPage, int pageSize, Case testCase) {
        //        创建分页对象，current为当前页数，size为每天最大记录数
        Page<Case> pageCase=new Page<>(currentPage,pageSize);
        QueryWrapper<Case> wrapperCase=new QueryWrapper<>();
//        第一个参数为是否执行条件，为true则执行该条件
        wrapperCase.like(StringUtils.isNullOrEmpty(testCase.getCasename()),"casename",testCase.getCasename());
//        根据项目id查询
        wrapperCase.eq(testCase.getId()>0,"id",testCase.getId());
//        id倒叙
        wrapperCase.orderByDesc("id");
//       调用分页查询方法，传入分页对象--pageProject，wrapperProject是构造条件对象
        return caseDao.selectPage(pageCase,wrapperCase);

    }

    @Override
    public List<Case> getCaseAll() {
        QueryWrapper<Case> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return caseDao.selectList(queryWrapper);
    }
}
